/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

using VSTrid = Microsoft.VisualStudio.VirtualTreeGrid;

using DslEditorPowerToy.Controls.VirtualTreeGrid;

using DslEditorPowerToy.Controls.Editors.Special.DomainModelTreeGrid.Branches;
using DslEditorPowerToy.Controls.Editors.Special.DomainModelTreeGrid.Data;
using Microsoft.VisualStudio.VirtualTreeGrid;

namespace DslEditorPowerToy.Controls.Editors.Special.DomainModelTreeGrid
{
    internal partial class DomainModelTreeGridControl : VirtualTreeGridControl
    {
        #region Constructors
        public DomainModelTreeGridControl()
            : base()
        {
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Re-initialises the tree control with specified object
        /// </summary>
        protected override void SetDataSource(object dataSource)
        {
            //Dereference selected object to get to the root tree element
            IDomainModel domainModel = null;
            if (dataSource is IDomainModel)
                domainModel = (IDomainModel)dataSource;
            else if (dataSource is IDomainRelationship)
            {
                IDomainRelationship relationship = (IDomainRelationship)dataSource;
                if (null != relationship)
                    domainModel = relationship.DomainModel;
            }
            else if (dataSource is IDomainClass)
            {
                IDomainClass domainClass = (IDomainClass)dataSource;
                if (null != domainClass)
                    domainModel = domainClass.DomainModel;
            }

            //Check value
            if (null != domainModel)
            {
                //Check if same root branch (optimization)
                if ((null != this.Tree)
                    && (null != this.Tree.Root))
                {
                    VSTrid.IBranch rootBranch = this.Tree.Root;
                    if (rootBranch is DomainModelBranch)
                    {
                        DomainModelBranch branch = (DomainModelBranch)rootBranch;
                        IDomainModel branchData = branch.BranchData;
                        if (true == branchData.Equals(domainModel))
                        {
                            //Dont need to reset the root branch, just select the object
                            base.SetDataSource(dataSource);
                            return;
                        }
                    }
                }

                //Set the new branch
                base.SetRoot(new DomainModelBranch(this, domainModel));
            }

            //Select the current object
            base.SetDataSource(dataSource);
        }
        #endregion
    }
}
